package com.think.teaching.controller;

import com.think.common.controller.BaseController;
import com.think.common.model.Result;
import com.think.common.model.ResultCode;
import com.think.teaching.model.Tag;
import com.think.teaching.service.impl.TagServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 标签 前端控制器
 * </p>
 *
 * @author think
 * @since 2020-08-12
 */
@RestController
@RequestMapping("/v1")
@ResponseBody
@Api(tags = "标签管理接口")
public class TagController extends BaseController {

	private final TagServiceImpl tagService;

	public TagController(TagServiceImpl tagService) {
		this.tagService = tagService;
	}

	@ApiOperation("添加新的标签")
	@PostMapping("admin/tag")
	public Result AddTag(@RequestBody() Tag data) {
		Result result = data.CheckObjectComplete();
		if (result.getCode() == ResultCode.SUCCESS.code()) {
			
			if(tagService.GetTagByTypeAndName(data.getType(),data.getTag()) == null) {
				Tag tag = tagService.AddTag(data);
				if (tag != null) {
					return Result.success(tag);
				} else {
					return Result.failure(ResultCode.SYSTEM_DATABASE_ERROR);
				}
			}
			else {
				return Result.failure(ResultCode.TAG_ALREADY_EXIST);
			}
			
		} else
			return result;
	}

	@ApiOperation("更新标签")
	@RequestMapping(value = "admin/tag", method = RequestMethod.PUT)
	public Result UpdateTag(@RequestBody() Tag data) {
		Result result = data.CheckObjectComplete();
		if (result.getCode() == ResultCode.SUCCESS.code()) {
			Tag tag =tagService.GetTagByTypeAndName(data.getType(),data.getTag());
			if(tag == null || tag.getId().equals(data.getId())) {
				tag = tagService.UpdateTag(data);
				if (tag != null) {
					return Result.success(tag);
				} else {
					return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
				}
			}
			else {
				return Result.failure(ResultCode.TAG_ALREADY_EXIST);
			}
			
		} else
			return result;
	}

	@ApiOperation("删除标签")
	@RequestMapping(value = "admin/tag/{id}", method = RequestMethod.DELETE)
	public Result DeleteTag(@PathVariable("id") Integer id) {
		Tag tag = tagService.GetTag(id,false);
		if (tag == null) {
			return Result.failure(ResultCode.TAG_NOT_EXIST);
		} else {
			if (tagService.DeleteObjectById(id)) {
				return Result.success();
			}
			return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
		}
	}

	@ApiOperation("管理员获取标签详情")
	@RequestMapping(value = "admin/tag/{id}", method = RequestMethod.GET)
	public Result AdminGetTag(@PathVariable("id") Integer id) {
		Tag tag = tagService.GetTag(id,false);
		if( tag== null) {
			return Result.failure(ResultCode.TAG_NOT_EXIST);
		}
		else {
			
			return Result.success(tag);
		}
	}
	
	@ApiOperation("用户获取标签详情")
	@RequestMapping(value = "user/tag/{id}", method = RequestMethod.GET)
	public Result GetTag(@PathVariable("id") Integer id) {
		Tag tag = tagService.GetTag(id,true);
		if( tag== null) {
			return Result.failure(ResultCode.TAG_NOT_EXIST);
		}
		else {
			
			return Result.success(tag);
		}
	}

	@ApiOperation("管理员修改标签状态")
	@RequestMapping(value = "admin/tag/{id}/{state}", method = RequestMethod.PUT)
	public Result UpdateTagState(@PathVariable("id") Integer id, @PathVariable("state") Integer state) {
		Tag tag = tagService.GetTag(id,false);
		if( tag== null) {
			return Result.failure(ResultCode.TAG_NOT_EXIST);
		}
		else {
			try {
				tag = tagService.SetTagState(id, state);
				return Result.success(tag);
			} catch (Exception e) {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		}
	}

	@ApiOperation("用户搜索标签")
	@RequestMapping(value = "user/tag/search", method = RequestMethod.GET)
	public Result UserSearchTag(@RequestParam("key") String key,@RequestParam("type")Integer type) {
		return Result.success(tagService.ListTagSearch(key,type, true));
	}

	@ApiOperation("管理员搜索标签")
	@RequestMapping(value = "admin/tag/search", method = RequestMethod.GET)
	public Result AdminSearchTag(@RequestParam("key") String key,@RequestParam("type") Integer type) {
		return Result.success(tagService.ListTagSearch(key,type, false));
	}

	@ApiOperation("管理员获取标签列表")
	@RequestMapping(value = "admin/tags/{page}/{count}", method = RequestMethod.GET)
	public Result AdminListTag(@PathVariable("page") Integer page, @PathVariable("count") Integer count,@RequestParam("type")Integer type) {
		return Result.success(tagService.ListTagWithTagAndType(count, page, type, false));
	}

	@ApiOperation("用户获取标签列表")
	@RequestMapping(value = "open/tags/{page}/{count}", method = RequestMethod.GET)
	public Result UserListTag(@PathVariable("page") Integer page, @PathVariable("count") Integer count,@RequestParam("type")Integer type) {
		return Result.success(tagService.ListTagWithTagAndType(count, page, type, true));
	}
}
